package com.yhq.basic.thread.juc;

import java.util.concurrent.CountDownLatch;

/**
 * @author hqyin
 * @date 2025/5/13 3:50 PM
 */
public class TestCountDown {
    public static void main(String[] args) {
        CountDownLatch count = new CountDownLatch(5);
        for (int i = 0; i < 5; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println(Thread.currentThread().getName()+"开始执行");
                        int waitTime = (int) (Math.random()*10000);
                        Thread.sleep(waitTime);
                        System.out.println(Thread.currentThread().getName()+"执行完成");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    } finally {
                        count.countDown();
                    }
                }
            },"线程"+i).start();
        }

        try {
            count.await();
            System.out.println("所有线程执行完成");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
